#include "Monster.h"

Monster::Monster(shared_ptr<Man > DigDug, shared_ptr<Soil > soil):

    _hole_up(false),
    _hole_down(false),
    _hole_left(false),
    _hole_right(false),

    _monster_state(0),
    _face_direction(RIGHT),
    _toward_target(false),
    _disembody(false),

    _random_bool(false),
    _random_number(0),
    _random_number2(0),
    _random_number3(0),

    _DigDug(DigDug),
    _soil(soil),

    _top_left(0),
    _top_right(0),
    _bottom_left(0),
    _bottom_right(0),

    _x_diff(0),
    _y_diff(0),
    _old_pos_x(0),
    _old_pos_y(0)

{}

void Monster::initialise()
{
    update_random_number();

    if ( _random_number == BOTTOM_LEFT )
    {
        update_bottom_left();
        LifeForm::initialise(_bottom_left, 500, 0,0,5,5, RED_MONSTER);
    }
    else if ( _random_number == TOP_RIGHT )
    {
        update_top_right();
        LifeForm::initialise(_top_right, 200, 0,0,5,5, RED_MONSTER);
    }
    else if ( _random_number == BOTTOM_RIGHT )
    {
        update_bottom_right();
        LifeForm::initialise(850, _bottom_right, 0,0,5,5, RED_MONSTER);
    }
    else if ( _random_number == TOP_LEFT )
    {
        update_top_left();
        LifeForm::initialise(200, _top_left, 0,0,5,5, RED_MONSTER);
    }
    _monster_state = ALIVE_NORMAL;

    check_soil();
}

void Monster::update()
{
    update_random_number3();

    if (_random_number3 == 5)
    {
        _monster_state = ALIVE_DISEMBODIED;
        _disembody = true;
    }

    if ( (_pos_x%50==0) && (_pos_y%50==0) )
    {
        check_soil();
    }

    if (_monster_state == ALIVE_NORMAL)
    {
        move_monster_norm();
        LifeForm::update();
    }

    else if (_monster_state == ALIVE_DISEMBODIED)
    {
        move_monster_disembodied();
        _pos_x += _speed_x * _direction_x;
        _pos_y += _speed_y * _direction_y;

        _LeftRight = true;
    }

    update_face_direction();

    if ( (_pos_x == -50)    ||
         (_pos_x == 1300)   ||
         (_pos_y == 50)     ||
         (_pos_y == 700)  )
    {
        set_alive(false);
    }

    if ( (_pos_x%50==0) && (_pos_y%50==0) && (_monster_state == ALIVE_DISEMBODIED) && (get_alive()) )
    {
        int col = _pos_x/50 + 1;
        int row = (_pos_y/50)-1;

        if (_soil->get_hole_tracker(col,row))
        {
            _monster_state = ALIVE_NORMAL;
            _disembody = false;
        }
    }

}

bool Monster::get_disembody()
{
    return _disembody;
}

int Monster::get_monster_state()
{
    return _monster_state;
}

void Monster::check_soil()
{
    int col = _pos_x/50 + 1;
    int row = (_pos_y/50)-1;

    if ( _soil -> get_hole_tracker(col,row-1) )
    {
        _hole_up = true;
    }
    else _hole_up = false;

    if ( _soil -> get_hole_tracker(col,row+1) )
    {
        _hole_down = true;
    }
    else _hole_down = false;

    if ( _soil -> get_hole_tracker(col-1,row) )
    {
        _hole_left = true;
    }
    else _hole_left = false;

    if ( _soil -> get_hole_tracker(col+1,row) )
    {
        _hole_right = true;
    }
    else _hole_right = false;
}

void Monster::move_monster_norm()
{
    update_random_bool();

    if ( _face_direction == UP )
    {
        if (_hole_up)
        {
            move_up();
        }
        else if (!_hole_up)
        {
            if (_random_bool && (_hole_left || _hole_right))
            {
                if (_hole_left)
                {
                    move_left();
                    still_y();
                }
                else if (_hole_right)
                {
                    move_right();
                    still_y();
                }
            }
            else if (!_random_bool && (_hole_left || _hole_right))
            {
                if (_hole_right)
                {
                    move_right();
                    still_y();
                }
                else if (_hole_left)
                {
                    move_left();
                    still_y();
                }
            }
            else move_down();
        }
    }

    else if ( _face_direction == DOWN )
    {
        if (_hole_down)
        {
            move_down();
        }
        else if (!_hole_down)
        {
            if (_random_bool && (_hole_left || _hole_right))
            {
                if (_hole_left)
                {
                    move_left();
                    still_y();
                }
                else if (_hole_right)
                {
                    move_right();
                    still_y();
                }
            }
            else if (!_random_bool && (_hole_left || _hole_right))
            {
                if (_hole_right)
                {
                    move_right();
                    still_y();
                }
                else if (_hole_left)
                {
                    move_left();
                    still_y();
                }
            }
            else move_up();
        }
    }

    else if ( _face_direction == LEFT)
    {
        if (_hole_left)
        {
            move_left();
        }
        else if (!_hole_left)
        {
            if (_random_bool && (_hole_up || _hole_down))
            {
                if (_hole_up)
                {
                    move_up();
                    still_x();
                }
                else if (_hole_down)
                {
                    move_down();
                    still_x();
                }
            }
            else if (!_random_bool && (_hole_up || _hole_down))
            {
                if (_hole_down)
                {
                    move_down();
                    still_x();
                }
                else if (_hole_up)
                {
                    move_up();
                    still_x();
                }

            }
            else move_right();
        }
    }

    else if ( _face_direction == RIGHT )
    {
        if (_hole_right)
        {
            move_right();
        }
        else if (!_hole_right)
        {
            if (_random_bool && (_hole_up || _hole_down))
            {
                if (_hole_up)
                {
                    move_up();
                    still_x();
                    //_face_direction = UP;
                }
                else if (_hole_down)
                {
                    move_down();
                    still_x();
                    //_face_direction = DOWN;
                }
            }
            else if (!_random_bool && (_hole_up || _hole_down))
            {
                if (_hole_down)
                {
                    move_down();
                    still_x();
                    //_face_direction = DOWN;
                }
                else if (_hole_up)
                {
                    move_up();
                    still_x();
                    //_face_direction = UP;
                }
            }
            else move_left();
        }
    }
}

void Monster::move_monster_disembodied()
{
    _x_diff = ( _DigDug->get_pos_x() - _pos_x);
    _y_diff = ( _DigDug->get_pos_y() - _pos_y);

    if( (_x_diff > 0) && (_x_diff != 0) )
    {
       move_right();
    }
    else if( (_x_diff) < 0 && (_x_diff != 0) )
    {
        move_left();
    }
    if( (_y_diff > 0) && (_y_diff != 0) )
    {
        move_down();
    }
    else if( (_y_diff < 0) && (_y_diff != 0) )
    {
        move_up();
    }
}

void Monster::update_random_bool()
{
    //srand( (unsigned)time(0) );
    int random_numb = rand();

    if(random_numb%2==1)    //odd
    {
        _random_bool = true;
    }

    else if (random_numb%2==0)  //even
    {
        _random_bool = false;
    }
}

void Monster::update_random_number()
{
    _random_number = rand() % 4+1;
}

void Monster::update_top_left()
{
    _random_number2 = rand() % 4+1;
    _top_left = _random_number2*50 + 150 ;
}

void Monster::update_top_right()
{
    _random_number2 = rand() % 4+1;
    _top_right = _random_number2*50 + 950 ;
}

void Monster::update_bottom_left()
{
    _random_number2 = rand() % 4+1;
    _bottom_left = _random_number2*50 + 200;
}

void Monster::update_bottom_right()
{
    _random_number2 = rand() % 4+1;
    _bottom_right = _random_number2*50 + 400;
}

void Monster::update_random_number3()
{
    //srand( (unsigned)time(0) );
    _random_number3  = rand() % 500;
}

void Monster::update_face_direction()
{
    if ( _direction_x > 0 )  //face right
    {
        _face_direction = RIGHT;
    }

    else if ( _direction_x < 0 ) //face left
    {
        _face_direction = LEFT;
    }

    else if ( _direction_y < 0)  //face up
    {
        _face_direction = UP;
    }

    else if ( _direction_y > 0 ) //face down
    {
        _face_direction = DOWN;
    }
}









